﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using System.IO;
using System.Net;

namespace SocialNetworksServices.Google.Contacts
{
    public class GContactsService
    {

        public GContactsService(string pAplicationName, string pLogin, string pPassword)
        {
            this.AplicationName = pAplicationName;
            this.Login = pLogin;
            this.Password = pPassword;
        }

        #region "Properties"
        public string AplicationName { get; set; }
        public string Login { get; set; }
        public string Password { get; set; }
        #endregion

        public List<Group> GetAllContactGroups()
        {
            RequestSettings rs = new RequestSettings(this.AplicationName, this.Login, this.Password);
            ContactsRequest contactRequest = new ContactsRequest(rs);
            Feed<Group> list = contactRequest.GetGroups();
            List<Group> returnList = new List<Group>();

            foreach (var item in list.Entries)
            {
                returnList.Add(item);
            }

            return returnList;
        }

        public List<Contact> GetAllContactsByGroup(string pGroupID)
        {
            RequestSettings rs = new RequestSettings(this.AplicationName, this.Login, this.Password);
            ContactsRequest contactRequest = new ContactsRequest(rs);

            ContactsQuery consulta = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
            consulta.Group = pGroupID;

            Feed<Contact> list = contactRequest.Get<Contact>(consulta);
            List<Contact> returnList = new List<Contact>();

            foreach (var item in list.Entries)
            {
                returnList.Add(item);
            }

            return returnList; ;
        }

        public Stream GetPhotoStreamByContact(string pContactID)
        {
            string chave = string.Empty;

            RequestSettings settings = new RequestSettings(this.AplicationName, this.Login, this.Password);

            chave = settings.OAuthUser;
            settings.AutoPaging = false;
            settings.Maximum = 50000;
            settings.PageSize = 50000;
            ContactsRequest req = new ContactsRequest(settings);

            chave = req.Service.QueryAuthenticationToken();

            ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));

            Feed<Contact> c = req.Get<Contact>(query);

            Contact contato = c.Entries.FirstOrDefault<Contact>();

            try
            {
                return req.GetPhoto(contato);
            }
            catch (GDataNotModifiedException ex)
            {
                HttpWebRequest reqWeb = (HttpWebRequest)HttpWebRequest.Create(contato.PhotoUri);
                reqWeb.Headers.Add("Authorization", "GoogleLogin auth=" + chave);
                HttpWebResponse resp = (HttpWebResponse)reqWeb.GetResponse();
                return resp.GetResponseStream();


            }
        }

        public Contact GetAllContactByID(string pContactID)
        {
            string chave = string.Empty;

            RequestSettings settings = new RequestSettings(this.AplicationName, this.Login, this.Password);

            ContactsRequest req = new ContactsRequest(settings);

            Contact c = req.Retrieve<Contact>(new Uri(pContactID));

            return c;
        }
    }
}
